<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /*实现一个字符串头尾去除空格的函数注意需要去除的空格，包括全角、半角空格暂时不需要学习和使用正则表达式的方式*/
        function diyTrim(str) {
            var result = "";
            let start = 0;
            // do something
            while (start < str.length && (str.split('')[start] === ' ' || str.split('')[start] === '　')) {
                start++;
            }
            str = str.substr(start, str.length);
            let end = str.length - 1;
            while (end > 0 && (str.split('')[end] === ' ' || str.split('')[end] === '　')) {
                end--;
            }
            result = str.substr(0, end + 1);
            return result
        }

        // 测试用例
        console.log(diyTrim(' a f b    ') === 'a f b'); // ->a f b
        console.log(diyTrim('    ffdaf    ') === 'ffdaf'); // ->ffdaf
        console.log(diyTrim('1    ') === '1'); // ->1
        console.log(diyTrim('　　f') === 'f'); // ->f
        console.log(diyTrim('  　  a f b 　　 ') === 'a f b'); // ->a f b
        console.log(diyTrim(' ') === ''); // ->
        console.log(diyTrim('　') === ''); // ->
        console.log(diyTrim('') === ''); // ->

        /*
        去掉字符串str中，连续重复的地方
        */
        function removeRepetition(str) {
            var result = "";
            var strA = str.split('');
            var strB = [];
            // do something
            for (let i = 0, len = strA.length; i < len; i++) {
                if (strA[i] !== strB[strB.length - 1]) {
                    strB.push(strA[i]);
                }
            }
            return strB.join('');
        }

        // 测试用例
        console.log(removeRepetition("aaa") === "a"); // ->a
        console.log(removeRepetition("abbba") === 'aba'); // ->aba
        console.log(removeRepetition("aabbaabb") === 'abab'); // ->abab
        console.log(removeRepetition("") === ''); // ->
        console.log(removeRepetition("abc") === 'abc'); // ->abc
    </script>
</body>

</html>